package com.t174.car.controller;

import com.t174.car.dao.*;
import com.t174.car.entity.*;
import com.t174.car.service.RepairOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/repairOrder")
@Scope("prototype")
public class RepairController {
    @Autowired
    RepairorderDao repairorderDao;
    @Autowired
    RepairorderinfoDao repairorderinfoDao;
    @Autowired
    RepairserviceDao repairserviceDao;
    @Autowired
    GuestsDao guestsDao;
    @Autowired
    GuestsCarDao guestsCarDao;
    @Autowired
    RepairOrder repairOrder;
    @Autowired
    ComponentsDao componentsDao;
    @RequestMapping("/query")
    @ResponseBody
    public List<Repairservice> getCarByGid(){
        return repairserviceDao.query();
    }
    @RequestMapping("/toOrder")
    public String toOrder(int cid,int gid,HttpServletRequest request){
        request.getSession().getServletContext().removeAttribute("g");
        request.getSession().getServletContext().removeAttribute("c");
        request.getSession().getServletContext().setAttribute("g", guestsDao.getGuestsById(gid));
        request.getSession().getServletContext().setAttribute("c", guestsCarDao.getCarById(cid,gid));
        return "redirect:/repair/repairOrder.jsp";
    }
    @RequestMapping("/addOrder")
    @ResponseBody
    public Integer addOrder(Repairorder repairorder,String orinfoId,HttpServletRequest request){
        UserInfo userInfo = (UserInfo) request.getSession().getServletContext().getAttribute("user");
        repairorder.setUid(userInfo.getId());
        System.out.println(repairorder.getMoeny());
        String[] ss = orinfoId.split("-");
        Integer[] rsId = new Integer[ss.length] ;
        for(int i=0;i<ss.length;i++){
            rsId[i] = Integer.parseInt(ss[i]) ;
        }
        int count = repairOrder.addOrder(repairorder,rsId);
        return count;
    }
    @RequestMapping("/getOrder")
    @ResponseBody
    public List<Repairorder> getOrder(Repairorder repairorder,HttpServletRequest request){
        UserInfo userInfo = (UserInfo) request.getSession().getServletContext().getAttribute("user");
        if(userInfo.getRole().getId()==4){
            repairorder.setStaff(-1);
            repairorder.setUid(-1);
            repairorder.setState(-1);
        }else{
            repairorder.setStaff(userInfo.getId());
            repairorder.setUid(userInfo.getId());
        }
        return repairorderDao.query(repairorder);
    }
    @RequestMapping("/getOrderInfo")
    @ResponseBody
    public List<Repairservice> queryOrderInfo(int id){
        return repairorderinfoDao.query(id);
    }
    @RequestMapping("/upOrderState")
    @ResponseBody
    public Integer upOrderState(int oid,int state){
        Double cmoney = repairorderinfoDao.getCmoney(oid);
        Double osmoney = repairorderinfoDao.getOSmoney(oid);
        if(cmoney==null){
            cmoney = 0.0;
        }
        if(osmoney==null){
            osmoney = 0.0;
        }
        Double money = cmoney+osmoney;
        return repairorderDao.upState(state,oid,money);
    }
    @RequestMapping("/subOrder")
    @ResponseBody
    public Integer orderAddC(RepairorderComponents repairorderComponents,String cid,String countStr){
        String[] cidStr = cid.split("-");
        String[] count = countStr.split("-");
        Integer[] cidInt = new Integer[cidStr.length];
        Integer[] countInt = new Integer[count.length];
        for (int i=0;i<cidStr.length;i++){
            cidInt[i] = Integer.parseInt(cidStr[i]);
            countInt[i] = Integer.parseInt(count[i]);
        }
        repairOrder.addOCinfo(repairorderComponents,cidInt,countInt);
        Double cmoney = repairorderinfoDao.getCmoney(repairorderComponents.getRoId());
        Double osmoney = repairorderinfoDao.getOSmoney(repairorderComponents.getRoId());
        Double money = cmoney+osmoney;
        return repairorderDao.upState(3,repairorderComponents.getRoId(),money);
    }
    @RequestMapping("/getByRoid")
    @ResponseBody
    public List<Components> getByRoid(int roid){
        return componentsDao.getByOrid(roid);
    }
    @RequestMapping("/backOrder")
    @ResponseBody
    public Integer backOrder(int oid,int state,String note){
        return repairorderDao.backState(state,oid, note);
    }
    @RequestMapping("/delService")
    @ResponseBody
    public Integer delService(String idStr){
        String[] ids = idStr.split("-");
        Integer[] id = new Integer[ids.length];
        for (int i=0;i<ids.length;i++){
            id[i] = Integer.parseInt(ids[i]);
        }
        return repairserviceDao.delService(id);
    }
    @RequestMapping("/addService")
    @ResponseBody
    public Integer addService(Repairservice repairservice){
        return repairserviceDao.addService(repairservice);
    }
    @RequestMapping("/upService")
    @ResponseBody
    public Integer upService(Repairservice repairservice){
        return repairserviceDao.upService(repairservice);
    }
}
